//
//  NSTxetAttachment+Emoticon.swift
//  01-keyBoard
//
//  Created by Leon on 15/10/25.
//  Copyright © 2015年 Leon. All rights reserved.
//

import UIKit

class EmoticonTextAttachment:NSTextAttachment {
    
 /// 表情的名称
    var chs: String?
    
    class func imageText(em: EmoticonModel, font: UIFont) -> NSAttributedString {
        // 1》取得图片附件
        let attach = EmoticonTextAttachment()
        attach.chs = em.chs
        attach.image = UIImage(named: em.imagePath)
        let lineHeight = font.lineHeight
        attach.bounds = CGRectMake(0, -4, lineHeight, lineHeight)
        
        // 2> 添加一个属性文本在image后面，字符不会变小
        let imageText = NSMutableAttributedString(attributedString: NSAttributedString(attachment: attach))
        
        imageText.addAttribute(NSFontAttributeName, value: font, range: NSRange(location: 0, length: 1))
        
        return imageText
    }
}